# coding=utf-8
# Author: hang.zhang
# Date: 2024-06-28 17:19:36
# LastEditors: hang.zhang
# LastEditTime: 2024-06-28 23:52:31


from dxlql import get_all_detail as dxlql_get_all_detail
from dxlql import get_all_detail_type2 as dxlql_get_all_detail_type2
from dxlql import get_zyz_detail as dxlql_get_zyz_detail
import pandas as pd
from datetime import datetime
def dxlql_parser():
    result = dxlql_get_all_detail()
    detail_result = []
    for page in result:
        for row in page["data"]["data"]["rows"]:
            if row["sf985"]:
                school_type = "985"
            elif row["sf211"]:
                school_type = "211"
            elif row["sfsyl"]:
                school_type = "双一流"
            else:
                school_type = "双非"
            base_line = {
                "学校": row["yxmc"],
                "自定义序号": None,
                "page_idx": page["page_idx"],
                "填报类型": page["myremark"],
                "办学类型": None,
                "临时备注": None,
                "学校类型": school_type,
                "代码": row["sb"],
                "地区": row["szdqmc"],
            }
            fee = None
            for zy in row["zys"]:
                line = base_line.copy()
                if "中外" in zy["zymc"]:
                    line["办学类型"] = "中外合作"
                else:
                    line["办学类型"] = "正常"
                zyid = zy["zyzid"]
                
                min_score = float('inf')
                min_index = float('inf')
                for sub in zy["subs"]:
                    if sub["nd"] == 2023:
                        year_2023_zdf = sub["zdtwf"]
                        year_2023_zdpm = sub["zdpm"]
                        year_2023_jhs = sub["jhs"]
                        cur_jsh = sub["jnjhrs"]
                    min_score = min(min_score, sub["zdtwf"] or 0)
                    min_index = max(min_index, sub["zdpm"] or 0)
                if min_score == float('inf'):
                    min_score = "-"
                if min_index == float('inf'):
                    min_index = "-"
                contain_zy = []
                for _zy in zy["bhzy2"].split(";"):
                    if "-" in _zy:
                        contain_zy.append(_zy.split("-")[1])
                contain_zy = "\n".join(contain_zy)
                if not cur_jsh or not year_2023_jhs:
                    diff_jsh = None
                else:
                    diff_jsh = cur_jsh - year_2023_jhs
                line.update({
                    "专业组": zy["zydm"],
                    "选科要求": zy["zxkmyq"],
                    "23年最低等效分": year_2023_zdf,
                    "23年最低位次": year_2023_zdpm,
                    "历年最低等效分": min_score,
                    "历年最低位次": min_index,
                    "去年招生人数": year_2023_jhs,
                    "今年招生人数": cur_jsh,
                    "浮动人数": diff_jsh,
                    "包含专业": contain_zy,
                    "专业组ID": zyid,
                })
                detail_result.append(line)

    df = pd.DataFrame(detail_result)
    df.to_excel("志愿填报导出.xlsx", index=False)

def dxlql_parser_all():
    result = dxlql_get_all_detail_type2()
    detail_result = []
    for page in result:
        for row in page["data"]["rows"]:
            if row["sf985"]:
                school_type = "985"
            elif row["sf211"]:
                school_type = "211"
            elif row["sfsyl"]:
                school_type = "双一流"
            else:
                school_type = "双非"
            base_line = {
                "学校": row["yxmc"],
                "自定义序号": None,
                "page_idx": page["page_idx"],
                # "填报类型": page["myremark"],
                
                "办学类型": None,
                "临时备注": None,
                "学校类型": school_type,
                "代码": row["sb"],
                "地区": row["szdqmc"],
            }
            fee = None
            for zy in row["zys"]:
                line = base_line.copy()
                if "中外" in zy["zymc"]:
                    line["办学类型"] = "中外合作"
                else:
                    line["办学类型"] = "正常"
                zyid = zy["zyzid"]
                
                min_score = float('inf')
                min_index = float('inf')
                for sub in zy["subs"]:
                    if sub["nd"] == 2023:
                        year_2023_zdf = sub["zdtwf"]
                        year_2023_zdpm = sub["zdpm"]
                        year_2023_jhs = sub["jhs"]
                        cur_jsh = sub["jnjhrs"]
                    min_score = min(min_score, sub["zdtwf"] or 0)
                    min_index = max(min_index, sub["zdpm"] or 0)
                if min_score == float('inf'):
                    min_score = "-"
                if min_index == float('inf'):
                    min_index = "-"
                contain_zy = []
                for _zy in zy["bhzy2"].split(";"):
                    if "-" in _zy:
                        contain_zy.append(_zy.split("-")[1])
                contain_zy = "\n".join(contain_zy)
                if not cur_jsh or not year_2023_jhs:
                    diff_jsh = None
                else:
                    diff_jsh = cur_jsh - year_2023_jhs
                line.update({
                    "专业组": zy["zydm"],
                    "选科要求": zy["zxkmyq"],
                    "23年最低等效分": year_2023_zdf,
                    "23年最低位次": year_2023_zdpm,
                    "历年最低等效分": min_score,
                    "历年最低位次": min_index,
                    "去年招生人数": year_2023_jhs,
                    "今年招生人数": cur_jsh,
                    "浮动人数": diff_jsh,
                    "包含专业": contain_zy,
                    "专业组ID": zyid,
                })
                detail_result.append(line)

    df = pd.DataFrame(detail_result)
    df.to_excel("志愿填报导出-全部-大学选专业.xlsx", index=False)


def get_from_subs(subs):
    year_2023_min_score = float('inf')
    year_2023_max_index = float('-inf')
    
    year_min_score = float('inf')
    year_max_index = float('-inf')
    
    for line in subs:
        cur_index = line["rdwc"] or float('-inf')
        cur_score = line["zdtwf"] or float('inf')
        year_min_score = min(year_min_score, cur_score)
        year_max_index = max(year_max_index, cur_index)
        if line["nd"] == (datetime.now().year - 1):
            year_2023_max_index = line["rdwc"]
            year_2023_min_score = line["zdtwf"]
    return year_2023_min_score, year_2023_max_index, year_min_score, year_max_index
        

def general_detail_major(excel_path):
    df = pd.read_excel(excel_path)
    final_df = pd.DataFrame()

    for index, row in list(df.iterrows()):
        new_row = row.copy()
        _id = row["专业组ID"]
        major_detail = dxlql_get_zyz_detail(_id)
        new_rows = []
        for major in major_detail["data"]:

            year_2023_min_score, year_2023_max_index, year_min_score, year_max_index = get_from_subs(major["subs"])
            cur_year = datetime.now().year - 1
            new_row["专业名字"] = major["zymc"]
            new_row["学费"] = major["sfbz"]
            new_row[f"{cur_year}年最低等效分数"] = year_2023_min_score
            new_row[f"{cur_year}年最高位次"] = year_2023_max_index
            new_row["近三年最低等效分"] = year_min_score
            new_row["近三年最高位次"] = year_max_index
            new_rows.append(new_row)
            final_df = final_df.append(new_row, ignore_index=True)
            # final_df = pd.concat([final_df] + new_rows, ignore_index=True)
    final_df.to_excel("带专业.xlsx", index=False)

def replace_br(excel_path):
    df = pd.read_excel(excel_path)
    for index, row in list(df.iterrows()):
        df.at[index, "包含专业"] = row["包含专业"].replace("\n", ";")
    df.to_excel(excel_path + "_new.xlsx")


if __name__ == "__main__":
    # dxlql_parser_smart()
    # dxlql_parser_all()
    # general_detail_major("/Users/zyt/Desktop/work/me/志愿填报/志愿填报导出-全部-大学选专业-zyl.xlsx")
    replace_br("/Users/zyt/Downloads/副本志愿填报导出-全部-大学选专业.xlsx")